package io.renren.modules.basic.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import io.renren.modules.basic.entity.*;
import io.renren.modules.basic.service.MtCcCategoryService;
import io.renren.modules.basic.service.MtCcProductService;
import io.renren.modules.basic.service.MtCcWarehousPositionRelatService;
import io.renren.modules.basic.service.MtCcWarehousRankService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: liguanghao
 * @Date: 2020/8/27 10:23
 * @Version 1.0
 */
@Api(tags = "大屏显示接口")
@RestController
@RequestMapping(value = "generator/screen")
public class screenController {

    @Autowired
    private MtCcWarehousRankService mtCcWarehousRankService;
    @Autowired
    private MtCcWarehousPositionRelatService mtCcWarehousPositionRelatService;
    @Autowired
    private MtCcCategoryService mtCcCategoryService;
    @Autowired
    private MtCcProductService mtCcProductService;

    /**
     * 全部已占用库位和未占用库位，显示百分比
     * @return
     */
    @ApiOperation( "全部已占用库位和未占用库位，显示百分比")
    @GetMapping("/countPosition")
    public R countPosition(){
        int all = 320;
        int notOccupy = mtCcWarehousRankService.count(new QueryWrapper<MtCcWarehousRankEntity>()
                .eq("is_occupy", 0).between("id", 1, 320));
        int isOccupy = all-notOccupy;
        int notOccupy12 = mtCcWarehousRankService.count(new QueryWrapper<MtCcWarehousRankEntity>()
                .eq("is_occupy", 0).between("id", 1, 80));
        int notOccupy78 = mtCcWarehousRankService.count(new QueryWrapper<MtCcWarehousRankEntity>()
                .eq("is_occupy", 0).between("id", 241, 320));
        int notOccupy3456 = mtCcWarehousRankService.count(new QueryWrapper<MtCcWarehousRankEntity>()
                .eq("is_occupy", 0).between("id", 81, 240));
        Map<String,Integer> data=new HashMap<>();
        data.put("all",all);
        data.put("notOccupy",notOccupy);
        data.put("isOccupy",isOccupy);
        data.put("notOccupy12",notOccupy12);
        data.put("notOccupy78",notOccupy78);
        data.put("notOccupy3456",notOccupy3456);
        return R.ok(data);
    }

    /**
     * 显示成品，半成品，原材料等大系列总库存数量和占用库位
     */
    @ApiOperation( "显示成品，半成品，原材料等大系列总库存数量和占用库位")
    @GetMapping("/countModel")
    public R countModel(){
        //成品占用库位数
        int countPositionFinishedProduct=0;
        int countQuantityFinishedProduct=0;
        List<MtCcWarehousPositionRelatEntity> finishedProduct = mtCcWarehousPositionRelatService
                .list(new QueryWrapper<MtCcWarehousPositionRelatEntity>()
                        .likeRight("product_code", "1."));
        countPositionFinishedProduct=finishedProduct.size();
        for (MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelatEntity:finishedProduct
             ) {
            Integer quantity = mtCcWarehousPositionRelatEntity.getQuantity();
            countQuantityFinishedProduct=countQuantityFinishedProduct+quantity;
        }
        //半成品占用库位数
        int countPositionSemiFinishedProduct=0;
        int countQuantitySemiFinishedProduct=0;
        List<MtCcWarehousPositionRelatEntity> semiFinishedProduct = mtCcWarehousPositionRelatService
                .list(new QueryWrapper<MtCcWarehousPositionRelatEntity>()
                        .likeRight("product_code", "3."));
        countPositionSemiFinishedProduct=semiFinishedProduct.size();
        for (MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelatEntity:semiFinishedProduct
        ) {
            Integer quantity = mtCcWarehousPositionRelatEntity.getQuantity();
            countQuantitySemiFinishedProduct=countQuantitySemiFinishedProduct+quantity;
        }
        //原材料占用库位数
        int countPositionRawMaterials=0;
        int countQuantityRawMaterials=0;
        List<MtCcWarehousPositionRelatEntity> rawMaterials = mtCcWarehousPositionRelatService
                .list(new QueryWrapper<MtCcWarehousPositionRelatEntity>()
                        .likeRight("product_code", "2."));
        countPositionRawMaterials=rawMaterials.size();
        for (MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelatEntity:rawMaterials
        ) {
            Integer quantity = mtCcWarehousPositionRelatEntity.getQuantity();
            countQuantityRawMaterials=countQuantityRawMaterials+quantity;
        }
        Map<String,Integer> data=new HashMap<>();
        data.put("countPositionFinishedProduct",countPositionFinishedProduct);
        data.put("countQuantityFinishedProduct",countQuantityFinishedProduct);
        data.put("countPositionSemiFinishedProduct",countPositionSemiFinishedProduct);
        data.put("countQuantitySemiFinishedProduct",countQuantitySemiFinishedProduct);
        data.put("countPositionRawMaterials",countPositionRawMaterials);
        data.put("countQuantityRawMaterials",countQuantityRawMaterials);
        return R.ok(data);
    }

    /**
     * 成品内所有类别小系列总库存数量和占用库位
     * @return
     */
    @ApiOperation( "成品内所有类别小系列总库存数量和占用库位")
    @GetMapping("/smallModel")
    public R smallModel(){
        List<MtCcCategoryEntity> categoryList = mtCcCategoryService.list();
        List<ScreenVO> screenVOList=new ArrayList<>();
        for (MtCcCategoryEntity categoryEntity:categoryList) {
            ScreenVO screenVO=new ScreenVO();
            int quantity=0;
            List<MtCcWarehousPositionRelatEntity> positionList =null;
            if ("1.01.".equals(categoryEntity.getName())){
                positionList = mtCcWarehousPositionRelatService
                        .list(new QueryWrapper<MtCcWarehousPositionRelatEntity>()
                                .likeRight("product_code", categoryEntity.getName())
                                .notLike("product_name","接头").notLike("product_name","连接器"));
            }else {
                positionList = mtCcWarehousPositionRelatService
                        .list(new QueryWrapper<MtCcWarehousPositionRelatEntity>()
                                .likeRight("product_code", categoryEntity.getName()));
            }

            for (MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelatEntity:positionList) {
                quantity=quantity+mtCcWarehousPositionRelatEntity.getQuantity();
            }
            screenVO.setCategoryName(categoryEntity.getCode());
            screenVO.setPosition(positionList.size());
            screenVO.setCategoryCode(categoryEntity.getName());
            screenVO.setQuantity(quantity);
            screenVOList.add(screenVO);
        }
        return R.ok(screenVOList);
    }

    /**
     * 显示盒底 盒盖等物料的总库存数量和占用库位总数量
     * @return
     */
    @ApiOperation( "显示盒底 盒盖等物料的总库存数量和占用库位总数量")
    @GetMapping("/coverAndBase")
    public R coverAndBase(){
        //盒底
        List<MtCcProductEntity> baseList = mtCcProductService.list(new QueryWrapper<MtCcProductEntity>()
                .like("product_name", "盒底"));
        //盒盖
        List<MtCcProductEntity> coverList = mtCcProductService.list(new QueryWrapper<MtCcProductEntity>()
                .like("product_name", "盒盖"));
        int basePosition=0;
        int baseQuantity=0;
        int coverPosition=0;
        int coverQuantity=0;
        for (MtCcProductEntity mtCcProductEntity:baseList) {
            List<MtCcWarehousPositionRelatEntity> mtCcWarehousPositionRelatEntities = mtCcWarehousPositionRelatService
                    .list(new QueryWrapper<MtCcWarehousPositionRelatEntity>()
                    .eq("product_code", mtCcProductEntity.getProductCode()));
            basePosition=basePosition+mtCcWarehousPositionRelatEntities.size();
            for (MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelatEntity:mtCcWarehousPositionRelatEntities) {
                baseQuantity=baseQuantity+mtCcWarehousPositionRelatEntity.getQuantity();
            }
        }
        for (MtCcProductEntity mtCcProductEntity:coverList) {
            List<MtCcWarehousPositionRelatEntity> mtCcWarehousPositionRelatEntities = mtCcWarehousPositionRelatService
                    .list(new QueryWrapper<MtCcWarehousPositionRelatEntity>()
                            .eq("product_code", mtCcProductEntity.getProductCode()));
            coverPosition=coverPosition+mtCcWarehousPositionRelatEntities.size();
            for (MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelatEntity:mtCcWarehousPositionRelatEntities) {
                coverQuantity=coverQuantity+mtCcWarehousPositionRelatEntity.getQuantity();
            }
        }
        Map<String,Integer> data=new HashMap<>();
        data.put("basePosition",basePosition);
        data.put("baseQuantity",baseQuantity);
        data.put("coverPosition",coverPosition);
        data.put("coverQuantity",coverQuantity);
        return R.ok(data);
    }
}
